package com.powernobug.service.impl;

import com.github.pagehelper.PageHelper;
import com.powernobug.bean.model.MarketCategory;
import com.powernobug.bean.model.MarketCategoryExample;
import com.powernobug.bean.vo.template.CategoryTempForHomeIndex;
import com.powernobug.mapper.MarketCategoryMapper;
import com.powernobug.annotation.PrintThreadLog;
import com.powernobug.bean.vo.data.GoodsListWithCategoryLevelData;
import com.powernobug.bean.vo.template.GoodsTempForHomeIndex;
import com.powernobug.service.MarketCategoryService;
import com.powernobug.service.MarketGoodsService;
import com.powernobug.util.TransferModelDataUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Future;

/**
 * @Author: 肖云戈
 * @CreateTime: 2024-08-30
 * @Description:
 */
@Service
public class MarketCategoryServiceImpl implements MarketCategoryService {

    @Autowired
    MarketCategoryMapper marketCategoryMapper;

    @Autowired
    MarketGoodsService marketGoodsService;

    @Override
    @Async("globalThreadPool")
    public Future<List<CategoryTempForHomeIndex>> getChannels() {
        MarketCategoryExample example = new MarketCategoryExample();
        example.createCriteria().andDeletedEqualTo(false);
        example.setOrderByClause("add_time desc");

        List<MarketCategory> originData = marketCategoryMapper.selectByExample(example);
        Class<?> resultType = CategoryTempForHomeIndex.class;
        List<CategoryTempForHomeIndex> resultData = (List<CategoryTempForHomeIndex>) TransferModelDataUtil.transferFatherToSon(resultType, originData);
        return new AsyncResult<>(resultData);
    }

    @Override
    @Async("globalThreadPool")
    @PrintThreadLog
    public Future<List<GoodsListWithCategoryLevelData>> getFloorGoodsListByLimit(int floorGoodsListLimit, int secondLevelGoodsLimit) {
        MarketCategoryExample example = new MarketCategoryExample();
        example.createCriteria().andDeletedEqualTo(false);
        example.setOrderByClause("add_time desc");
        PageHelper.startPage(1, floorGoodsListLimit);

        // 先查出一级类目
        List<MarketCategory> marketCategories = marketCategoryMapper.selectByExample(example);
        // 将一级类目中的id封装为一个List
        List<Integer> categoryIdList = marketCategories.stream()
                .map(MarketCategory::getId)
                .toList();
        // 通过一级类目的 id ，条件查询所有二级类目
        List<GoodsTempForHomeIndex> allSecondLevelGoodsList = marketGoodsService.getSecondLevelGoodsListByCategoryIdWithLimit(categoryIdList, secondLevelGoodsLimit);

        // 新建一个空的最终数据包装List
        List<GoodsListWithCategoryLevelData> floorGoodsList = new ArrayList<>();
        // 遍历一级类目
        for (MarketCategory category : marketCategories) {
            // 新建一个空的二级类目
            GoodsListWithCategoryLevelData floorGoods = new GoodsListWithCategoryLevelData();
            int categoryId = category.getId();

            // 封装二级类目
            floorGoods.setName(category.getName());
            // 将条件查询出的所有二级类目，通过流处理，过滤出遍历到的当前一级类目 id 的数据，然后封装到List中
            floorGoods.setGoodsList(allSecondLevelGoodsList
                    .stream()
                    .filter(x -> x.getCategoryId().equals(categoryId))
                    .toList());
            floorGoods.setId(categoryId);
            // 将封装好的层级数据add进最终数据包装list中
            floorGoodsList.add(floorGoods);
        }
        return new AsyncResult<>(floorGoodsList);
    }
}
